package com.xiaoyu.lanling.feature.coin.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.coin.model.CoinProductItem
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.util.TextViewUtil

class CoinProductViewHolder : ViewHolderBase<CoinProductItem>() {

    companion object {
        val sOnClickListener = View.OnClickListener { v ->
            val item = v.getViewTag<CoinProductItem>() ?: return@OnClickListener
            val activity = App.getInstance().topActivity ?: return@OnClickListener
            Router.instance.gotoCoinChargeActivity(activity, item)
        }
    }

    private lateinit var mTitle: TextView
    private lateinit var mPrice: TextView
    private lateinit var mChargeTag: TextView

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_coin_product, parent, false)
        mTitle = view.findViewById(R.id.title)
        mPrice = view.findViewById(R.id.price)
        mChargeTag = view.findViewById(R.id.charge_tag)

        mPrice.setOnClickDebounceListener(sOnClickListener)

        return view
    }

    override fun showData(position: Int, itemData: CoinProductItem) {
        TextViewUtil.displayOrGone(mChargeTag, itemData.chargeTag)
        mTitle.text = itemData.subject
        mPrice.text = AppContext.getString(R.string.coin_product_price_prefix, itemData.price)
        mPrice.setViewTag(itemData)
    }
}